var panels = [{
    id: 'p1',
    title: '本月全行各网点绩效排名前十',
    height: 435,
    content: '<table id="counterListTable"></table>'
}, {
    id: 'p2',
    title: '本月全行各人员绩效排名前十',
    height: 435,
    content: '<table id="personListTable"></table>'
}];

function getPanelOptions(id) {
    for (var i = 0; i < panels.length; i++) {
        if (panels[i].id == id) {
            return panels[i];
        }
    }
    return undefined;
}

function getPortalState() {
    var aa = [];
    for (var columnIndex = 0; columnIndex < 3; columnIndex++) {
        var cc = [];
        var panels = $('#homePortal').portal('getPanels', columnIndex);
        for (var i = 0; i < panels.length; i++) {
            cc.push(panels[i].attr('id'));
        }
        aa.push(cc.join(','));
    }
    return aa.join(':');
}

function addPanels(portalState) {
    var columns = portalState.split(':');
    for (var columnIndex = 0; columnIndex < columns.length; columnIndex++) {
        var cc = columns[columnIndex].split(',');
        for (var j = 0; j < cc.length; j++) {
            var options = getPanelOptions(cc[j]);
            if (options) {
                var p = $('<div/>').attr('id', options.id).appendTo('body');
                p.panel(options);
                $('#homePortal').portal('add', {
                    panel: p,
                    columnIndex: columnIndex
                });
            }
        }
    }
}

//初始化表格
function initDatagrid() {
    var url = $.util.getRequestJson('/json/statistics.allPersonRank.json');
    url = url + "?type=1&rows=10";
    $('#personListTable').datagrid({
        url: url,
        idField: 'id',
        fit: true,
        border: false,
        fitColumns: true,
        checkOnSelect: false,
        selectOnCheck: false,
        singleSelect: true,
        columns: [
            [
                {
                    field: 'rank',
                    title: '排名',
                    align: 'center'
                },
                {
                    field: 'name',
                    title: '姓名',
                    width: 2
                },
                {
                    field: 'tellerNo',
                    title: '柜员号',
                    align: 'center',
                    width: 2
                },
                {
                    field: 'counterName',
                    title: '所属网点',
                    width: 4
                },
                {
                    field: 'score',
                    title: '绩效得分',
                    width: 1
                }
            ]
        ]
    });
     url = $.util.getRequestJson('/json/statistics.counterRank.json');
    $('#counterListTable').datagrid({
        url: url+"?type=1&rows=10",
        idField: 'id',
        fit: true,
        fitColumns: true,
        border: false,
        checkOnSelect: false,
        selectOnCheck: false,
        singleSelect: true,
        columns: [
            [
                {
                    field: 'rank',
                    title: '排名',
                    align: 'center'
                },
                {
                    field: 'counterNo',
                    title: '网点编号',
                    width: 2
                },
                {
                    field: 'counterName',
                    title: '网点名称',
                    width: 5
                },
                {
                    field: 'score',
                    title: '绩效得分',
                    width: 2
                }
            ]
        ]
    });
}

$(function () {
    $('#homePortal').portal({
        fit: true,
        border: false,
        onStateChange: function () {
            var state = getPortalState();
            var date = new Date();
            date.setTime(date.getTime() + 24 * 3600 * 1000);
            document.cookie = 'portal-state=' + state + ';expires=' + date.toGMTString();
        }
    });
    var state = $.util.cookie('portal-state');
    if (!state) {
        state = 'p1:p2';
    }
    addPanels(state);
    setTimeout("$('#homePortal').portal('resize')", 100);
    // initDatagrid();
});
